package com.example.common.system.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.example.common.core.entity.BaseEntity;
import com.example.common.system.entity.RoleEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author chenjf
 * @Date 2022/9/20
 * @Description 用于处理userdetail类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser extends BaseEntity implements UserDetails , Serializable {

    private Long id;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 手机号
     */
    private String phone;
    /**
     * 是否到期
     */
    private Boolean accountNonExpired;
    /**
     * 是否被锁定
     */
    private Boolean accountNonLocked;
    /**
     * 盐
     */
    private String salt;
    /**
     * 启动状态
     */
    private Boolean status;
    /**
     * 最后登录时间
     */
    private Date lastLoginTime;
    /**
     * 是否启用[0启用1未启用]
     */
    private Boolean enabled;
    /**
     * 用户凭证是否过期
     */
    private Boolean credentialsNonExpired;

    /**
     * 角色对象
     */
    private List<RoleEntity> roles;

    /**
     * 权限列表
     */
    private Set<String> permissions;

//    @ApiModelProperty(value = "超级管理员   0：否   1：是")
//    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Integer superAdmin;

//    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public boolean isAdmin(){
        return getId() == 1;
    }
}
